package com.yzx.lifeassistants.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.GetListener;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.base.BaseApplication;
import com.yzx.lifeassistants.bean.RunningInfo;
import com.yzx.lifeassistants.bean.User;
import com.yzx.lifeassistants.utils.LoadLocalImageUtil;
import com.yzx.lifeassistants.view.RoundedImageView;

/**
 * @Description: 约跑列表的适配器
 * @author: yzx
 * @time: 2015-9-15 下午1:54:14
 */
public class RunningAdapter extends BaseAdapter {
	private Context context;
	private List<RunningInfo> dataList;
	private RunningInfo data;
	private LayoutInflater inflater;
	private ViewHolder holder;
	private List<Boolean> isList;
	private Boolean is;
	private ListView listView;

	public RunningAdapter(ListView listView, Context context,
			List<RunningInfo> dataList, List<Boolean> isList) {
		this.listView = listView;
		this.context = context;
		this.dataList = dataList;
		this.isList = isList;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("HandlerLeak")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_running_listview,
					parent, false);
			holder = new ViewHolder();
			holder.avatorIV = (RoundedImageView) convertView
					.findViewById(R.id.item_running_user_avator_iv);
			holder.nickTV = (TextView) convertView
					.findViewById(R.id.item_running_user_nick_tv);
			holder.updateTV = (TextView) convertView
					.findViewById(R.id.item_running_update_time_tv);
			holder.describeTV = (TextView) convertView
					.findViewById(R.id.item_running_describe_tv);
			holder.timeTV = (TextView) convertView
					.findViewById(R.id.item_running_time_tv);
			holder.placeTV = (TextView) convertView
					.findViewById(R.id.item_running_place_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		data = dataList.get(position);
		is = isList.get(position);
		if (is) {
			holder.nickTV.setTextColor(Color.RED);
			holder.describeTV.setTextColor(Color.RED);
		} else {
			holder.nickTV.setTextColor(Color.BLACK);
			holder.describeTV.setTextColor(Color.BLACK);
		}
		holder.avatorIV.setTag(position + "avator");
		holder.nickTV.setTag(position + "nick");
		loadUserInfo(position);
		holder.updateTV.setText(data.getUpdatedAt().substring(0,
				data.getUpdatedAt().indexOf(" ")));
		holder.describeTV.setText(data.getDescribe());
		holder.timeTV.setText("时间：" + data.getTime());
		holder.placeTV.setText("地点：" + data.getPlace());
		return convertView;
	}

	/**
	 * 
	 * @Description: 加载用户昵称和头像
	 */
	private void loadUserInfo(final int position) {
		BmobQuery<User> query = new BmobQuery<User>();
		query.getObject(context, data.getUser().getObjectId(),
				new GetListener<User>() {

					@Override
					public void onFailure(int code, String message) {
						ImageView avator = (ImageView) listView
								.findViewWithTag(position + "avator");
						TextView nick = (TextView) listView
								.findViewWithTag(position + "nick");
						if (null != avator) {
							LoadLocalImageUtil.getInstance()
									.displayFromDrawable(
											R.drawable.user_icon_default_main,
											avator);
						}
						if (null != nick) {
							nick.setText(dataList.get(position).getUsername());
						}
					}

					@Override
					public void onSuccess(User userInfo) {
						ImageView avator = (ImageView) listView
								.findViewWithTag(position + "avator");
						TextView nick = (TextView) listView
								.findViewWithTag(position + "nick");
						if (userInfo != null) {
							if (null != nick) {
								if (null == userInfo.getNick()
										&& "".equals(userInfo.getNick())) {
									nick.setText(userInfo.getUsername());
								} else {
									nick.setText(userInfo.getNick());
								}
							}
							if (null != avator) {
								if (null == userInfo.getAvatar()) {
									LoadLocalImageUtil
											.getInstance()
											.displayFromDrawable(
													R.drawable.user_icon_default_main,
													avator);
								} else {
									ImageLoader
											.getInstance()
											.displayImage(
													userInfo.getAvatar()
															.getFileUrl(context),
													avator,
													BaseApplication
															.getInstance()
															.getOptions(
																	R.drawable.user_icon_default_main),
													new SimpleImageLoadingListener() {
														@Override
														public void onLoadingComplete(
																String imageUri,
																View view,
																Bitmap loadedImage) {
															super.onLoadingComplete(
																	imageUri,
																	view,
																	loadedImage);
														}
													});
								}
							}

						}
					}
				});
	}

	static class ViewHolder {
		RoundedImageView avatorIV;// 发布者头像
		TextView nickTV;// 发布者昵称
		TextView updateTV;// 更新时间
		TextView describeTV;// 描述
		TextView timeTV;// 时间
		TextView placeTV;// 地点
	}

}
